package gui;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

/**
 * A class to handle key-inputs for the GUI.
 * @author nick-luca.frank
 *
 */
public class GUIkeyListener extends KeyAdapter {
	private ArrayList<Integer> keys;
	private ArrayList<Integer> releasedKeys;
	
	/**
	 * Initializes ArrayLists for keys and keys that got released.
	 */
	public GUIkeyListener(){
		keys = new ArrayList<Integer>();
		releasedKeys = new ArrayList<Integer>();
	}
	
	/**
	 * Registers key-presses and adds them to the according ArrayList.
	 */
	public void keyPressed(KeyEvent e){
		keys.add(e.getKeyCode());
	}
	
	/**
	 * Registers key-releases, removes the released keys from the keys ArrayList and adds them to the releasedKeys ArrayList.
	 */
 	public void keyReleased(KeyEvent e){
 		releasedKeys.add(e.getKeyCode());
 		while(keys.contains((Integer)e.getKeyCode())){
 			keys.remove((Integer)e.getKeyCode());
 		}
 	}
 	/**
 	 * Checks if a key is in the keys ArrayList.
 	 * @param key the key-code
 	 * @return whether the key is in the keys ArrayList or not
 	 */
	public boolean isDown(int key){
		return keys.contains(key);
	}
	
	/**
	 * Check whether the releasedKeys ArrayList is empty.
	 * @return whether the releasedKeys ArrayList is empty or not
	 */
	public boolean isQueueEmpty(){
		return releasedKeys.isEmpty();
	}
	
	/**
	 * Removes the newest key in the releasedKeys ArrayList.
	 * @return the removed key
	 */
	public int getQueuedKey(){
		int releasedKey = releasedKeys.get(releasedKeys.size() - 1);
		releasedKeys.remove(releasedKeys.size() - 1);
		return releasedKey;
	}
}

